package cn.xiao.permission_system.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动填充
 * @author beth
 * @data 2019-10-22 08:38
 */
@Component
public class MyMetaObjcetHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        boolean hasCreateTime = metaObject.hasSetter("createTime");
        if(hasCreateTime){
           setInsertFieldValByName("createTime", LocalDateTime.now(),metaObject);
        }
        boolean hasUpdateTime = metaObject.hasSetter("updateTime");
        if(hasUpdateTime){
            setInsertFieldValByName("updateTime", LocalDateTime.now(),metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object val = getFieldValByName("updateTime",metaObject);
        if(val == null){
            setUpdateFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        }
    }
}
